<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/11 0011
 * Time: 13:08
 */
namespace app\index\controller;

use think\Controller;
use app\index\model\WxUserModel;
use think\Db;
use think\Validate;

class Home extends Controller
{
    public function password(){
        $userInfo = session("user");

        if(empty($userInfo)){
            $this->error("请退出登录");
        }
        if($this->request->isPost()){
            $data = $this->request->param();
            $validate = new Validate([
                'password' => 'require'
            ],[
                'password'=>"密码不能为空"
            ]);

            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $info = Db::name("front_user")->where("id",$userInfo['id'])->update(['password'=>md5($data['password']),'utime'=>time()]);
            if(empty($info)){
                $this->error("修改失败");
            }

            $this->success("修改成功",url("index/Home/index",[],true,true));

        }
    }
    //登录业
    public function login(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $validate = new Validate([
                'user_name'  => 'require|max:25',
                'password' => 'require'
            ],[
                'user_name'=>"帐号不能为空",
                'password'=>"密码不能为空"
            ]);

            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $info = Db::name("front_user")->where("username",$data['user_name'])->find();
            if(empty($info)){
                $this->error("帐号错误");
            }
            $pw = md5($data['password']);
            if($pw != $info['password']){
                $this->error("密码错误");
            }

            session("user",$info);
            $this->success("登录成功",url("index/Home/index",[],true,true));

        }
        return $this->fetch();
    }
    //首页
    public function index(){
        $userInfo = session("user");

        if(empty($userInfo)){
            $this->redirect(url("index/Home/Login"));
        }
        $info =Db::name("front_user")->where("id",$userInfo['id'])->find();
        $this->assign("info",$info);
        return $this->fetch();
    }
    //明细
    public function mingxi(){
        $userInfo = session("user");
        if(empty($userInfo)){
            $this->redirect(url("index/Home/Login"));
        }
        return $this->fetch();
    }
    //明细
    public function mingxiInfo(){
        $userInfo = session("user");
        $user_id=isset($userInfo['id'])?$userInfo['id']:0;
        $param =$this->request->param();
        $data = array_filter($param);
        $map['a.user_id']=$user_id;
        if(isset($data['start_time'])&&!isset($data['end_time'])){
            $map['a.ctime']=['>',strtotime($data['start_time']." 00:00:00")];
        }
        if(isset($data['end_time'])&&!isset($data['start_time'])){
            $map['a.ctime']=['<',strtotime($data['end_time']." 23:59:59")];
        }
        if(isset($data['start_time'])&&isset($data['end_time'])){
            $a=strtotime($data['start_time']." 00:00:00");
            $b=strtotime($data['end_time']." 23:59:59");
            $map['a.ctime'] = ["between",[$a,$b]];
        }
        $selectResult = Db::name("balance")->alias('a')->field( 'a.*,xl_user.user_name as admin_name, xl_front_user.username')
            ->join('xl_user',  'a.admin_id = xl_user.id')
            ->join('xl_front_user',  'a.user_id = xl_front_user.id')
            ->where($map)->order('id desc')->select();
        // 拼装参数
        $status = config('balance_types');
        $to =0;
        foreach($selectResult as $key=>$vo){

            $a="";
            switch($vo['types']){
                case 1:

                    $a = $vo['new_money'];
                    $to+=$a;
                    break;
                case 2:
                    $a = $vo['new_money'];
                    $to-=$a;
                    break;
                default:
                    $a="";
            }

            $a = empty($a)?"":$a;
            $selectResult[$key]['s'] = $a;
            $selectResult[$key]['ctime'] = date("Y-m-d",$vo['ctime']);
            $selectResult[$key]['ts'] = $vo['types'];
            $selectResult[$key]['types'] = $status[$vo['types']];

            if( 1 == $vo['id'] ){
                $selectResult[$key]['operate'] = '';
                continue;
            }
            //$selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
        }
        $this->success($to,"",$selectResult);

    }
}